package com.situ.mall.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.situ.mall.admin.mapper.UserMapper;
import com.situ.mall.admin.util.RequestUtil;
import com.situ.mall.admin.vo.ResultVO;
import com.situ.mall.admin.vo.TableVO;
import com.situ.mall.admin.entity.User;
import com.situ.mall.admin.service.IUserService;
import com.situ.mall.common.exception.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.regex.Pattern;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lwj
 * @since 2023-09-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Autowired
    private UserMapper userMapper;
    @Override
    public TableVO select(Integer page, Integer limit, String username, String name, String status) {

        PageHelper.startPage(page,limit);

        return TableVO.data(userMapper.list(username,name,status));
    }

    @Override
    public void save2(User u) {
        if(u.getId()==null){
            User user = userMapper.selectByUserName(u.getUsername());
            if(user != null){
                throw new MyException("用户名重复！");
            }
            u.setCreateTime(LocalDateTime.now());
            userMapper.insert(u);

        }else {
            userMapper.update(u);
        }

    }

    @Override
    public User info(Integer id) {
        return userMapper.edit(id);

    }

    @Override
    public void delete(Integer id) {
        String s = userMapper.selectById(id);
        if(s.equals("admin")){
            throw new MyException("这是管理员账号，不能删除！");
        }else {
            userMapper.delete(id);
        }
    }

//    @Override
//    public boolean check(String username, String password, HttpSession session) {
//        User u = userMapper.selectByUserName(username);
//        boolean b =u!=null && u.getPassword().equals(password);
//        // TODO 密码加密
//        if(b){
//            session.setAttribute("currentUser",u);
//        }
//        return b;
//    }

    @Override
    public boolean check(String username, String password) {
        User u = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername,username));
        boolean b = u!=null && u.getPassword().equals(password);//StringUtil.md5(password));
        if(b){
            RequestUtil.setCurrent(u);
        }
        return  b;
    }

    @Override
    public ResultVO checkPassword(String password, String password1) {
        ResultVO vo = new ResultVO();
        if(!password.equals(password1)){//两次密码相等
            vo.setCode(0);
        }else{
            vo.setCode(1);
        }
        return vo;
    }

    //判断重置密码的密码格式，长度
    @Override
    public ResultVO resetPassword(String password1, String password2) {
        ResultVO vo = new ResultVO();
        User u = (User) RequestUtil.getCurrent();
        boolean b = checkPasswordFormat(password1);
        if(password1.length()<6){
            vo.setCode(3);
        }
        else if (!b){
            vo.setCode(2);
        }
        else if(!password1.equals(password2)){
            vo.setCode(0);
        }else{
            userMapper.resetPassword(password1,u.getId());
            vo.setCode(1);

        }
        return vo;
    }


    //检查新密码的密码格式
    private boolean checkPasswordFormat(String password1) {
        String pattern = "^(?=.*[0-9])(?=.*[a-zA-Z])[0-9A-Za-z]{6,}$";
        return Pattern.matches(pattern, password1);
    }

    //批量删除
    @Override
    public void delete2(String ids) {
        //进来的是String类型数组 遍历数组
//        for (String id: ids.split(",")) {
//            userMapper.delete(Integer.parseInt(id));
//        }
        userMapper.delete2(ids.split(","));
    }
}
